<?php
namespace Orm;
/**
 * model基类
 */
use think\Model;
class BaseModels extends Model
{
    public function __construct($data = [])
    {
        $name  = str_replace('\\', '/', static::class);
        $exp = explode('Model',$name);
        if(empty($exp[0])){
            throw new \Exception('缺少Model');
        }
        $name = strtolower(preg_replace('/(?<=[a-z])([A-Z])/', '_$1', $exp[0]));
        $this->name = $name;
        parent::__construct($data);
    }

    /**
     * 单个表分页获取数据
     * total：总条数，per_page:单页条量，current_page：当前页，last_page：总页数，data：分页数据
     * @param $page
     * @param int $limit
     * @param array $where
     * @return array
     * @throws \think\db\exception\DbException
     */
    public function singleGetList($page, $limit = 0, $where = [], $field = '', $order = '')
    {
        $limit = empty( $limit ) ? 4 : $limit;
        $order = empty( $order ) ? 'id desc' : $order;
        $field =  empty( $limit ) ? '*' : $field;
        $list = $this->where($where)->field($field)->order($order)->paginate($limit, false, [
            'page' => $page
        ])->toArray();
        $list['list'] = $list['data'];
        unset( $list['data']);
        return $list;
    }


}